﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class StaffSalaryService : IStaffSalaryService
    {
        private IStaffSalaryDal _staffSalary;
        private IMapper _mapper;
        public StaffSalaryService(IStaffSalaryDal staffSalary, IMapper mapper)
        {
            _staffSalary = staffSalary;
            _mapper = mapper;
        }

        public async Task<int> AddStaffSalaryAsync(decimal salary, int staffId)
        {
            return await _staffSalary.AddAsync(new Staffsalary 
            {
                Salary = salary,
                StaffId = staffId   //员工外键
            });
        }

        public async Task<int> DeleteStaffSalaryAsync(int id)
        {
            var data = await _staffSalary.QueryAsync(id);
            return data == null ? -1 : await _staffSalary.DeleteAsync(data);
        }

        public async Task<int> EditStaffSalaryAsync(int id, decimal salary, int staffId)
        {
            var data = await _staffSalary.QueryAsync(id);
            if (data == null)
                return -1;
            data.Salary = salary;
            data.StaffId = staffId;
            return await _staffSalary.EditAsync(data);
        }

        public async Task<List<StaffSalaryDto>> QueryStaffSalaryAllAsync()
        {
            var data = await _staffSalary.QueryAsync();
            return _mapper.Map<List<Staffsalary>,List<StaffSalaryDto>>(data);
        }

        public async Task<StaffSalaryDto> QueryStaffSalaryByIdAsync(int id)
        {
            var data = await _staffSalary.QueryAsync(id);
            return _mapper.Map<Staffsalary,StaffSalaryDto>(data);
        }

        public async Task<List<StaffSalaryDto>> QueryStaffSalaryByNameAsync(int staffId)
        {
            var data = await _staffSalary.QueryAsync(sf => sf.StaffId == staffId);
            return _mapper.Map<List<Staffsalary>, List<StaffSalaryDto>>(data);
        }
    }
}
